package com.kingsoft.dc.khaos.module.mpp.utils;


import com.kingsoft.dc.khaos.extender.meta.api.AuthRequest;
import com.kingsoft.dc.khaos.extender.meta.api.DmRequest;
import com.kingsoft.dc.khaos.extender.meta.model.MetaParams;
import com.kingsoft.dc.khaos.extender.meta.model.ds.DmDatasource;
import com.kingsoft.dc.khaos.extender.meta.model.ds.MppConnect;
import com.kingsoft.dc.khaos.extender.meta.utils.DmMetaUtils;
import com.kingsoft.dc.khaos.module.mpp.client.Connect;
import com.kingsoft.dc.khaos.module.mpp.metadata.MetaConfig;
import com.kingsoft.dc.khaos.plugin.core.KhaosContext;
import com.kingsoft.dc.khaos.plugin.core.constants.ScheduleConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by jing on 19/8/1.
 */
public class MetaUtils {
    private static Logger logger = LoggerFactory.getLogger(MetaUtils.class);


    /**
     * 从数据管理获取连接信息
     * @param metaConfig
     * @return
     */
    private static MppConnect getConnectFromDm(KhaosContext kc, MetaConfig metaConfig) {
        logger.info("开始获取数据源连接信息...");
        DmRequest request = buildWriteDsRequest(kc, metaConfig);
        DmDatasource datasource = DmMetaUtils.getDatasourceByName(metaConfig.getClazz(), request);
        logger.info("成功获取数据源连接信息...");
        return (MppConnect)datasource.getConnect();
    }

    /**
     * 获取连接信息
     * @param metaConfig
     * @return
     */
    public static Connect getConnect(KhaosContext kc, MetaConfig metaConfig){
        MppConnect mppConnect = getConnectFromDm(kc, metaConfig);
        Connect connect = new Connect();
        connect.setInstanceName(mppConnect.getInstanceName());
        connect.setHost(mppConnect.getHost());
        connect.setPort(mppConnect.getPort());
        connect.setUsername(mppConnect.getUsername());
        connect.setPassword(mppConnect.getPassword());
        return connect;
    }




    /**
     * 获取
     * @param kc
     * @param metaConfig
     * @return
     */
    public static DmRequest buildReadDsRequest(KhaosContext kc, MetaConfig metaConfig) {
        DmRequest request = buildMetaQueryRequest(kc);
        MetaParams metaParams = new MetaParams();
        metaParams.buildReadDatasourceParams(kc.getConf().getString(ScheduleConstants.RUN_ENV), metaConfig.getParams().getDsName());
        request.setMetaParams(metaParams);
        return request;
    }

    public static DmRequest buildWriteDsRequest(KhaosContext kc, MetaConfig metaConfig) {
        DmRequest request = buildMetaQueryRequest(kc);
        MetaParams metaParams = new MetaParams();
        String env = kc.getConf().getString(ScheduleConstants.RUN_ENV);
        metaParams.buildWriteDatasourceParams(env, metaConfig.getParams().getDsName());
        request.setMetaParams(metaParams);
        return request;
    }

    private static DmRequest buildMetaQueryRequest(KhaosContext kc) {
        DmRequest request = new DmRequest();
        AuthRequest authRequest = new AuthRequest();
        authRequest.setAuthUrl(kc.conf().getString(ScheduleConstants.AUTH_SERVER_KEY));
        authRequest.setAuthProjectId(kc.conf().getInt(ScheduleConstants.PROJECT_ID, 0));
        request.setUrl(kc.conf().getString(ScheduleConstants.META_QUERY_URL_KEY));
        request.setAuthRequest(authRequest);
        return request;
    }


    public static DmRequest buildRightQueryRequest(KhaosContext kc) {
        DmRequest request = new DmRequest();
        AuthRequest authRequest = new AuthRequest();
        authRequest.setAuthUrl(kc.conf().getString(ScheduleConstants.AUTH_SERVER_KEY));
        authRequest.setAuthProjectId(kc.conf().getInt(ScheduleConstants.PROJECT_ID, 0));
        request.setUrl(kc.conf().getString(ScheduleConstants.META_RIGHT_QUERY_URL_KEY));
        request.setAuthRequest(authRequest);
        return request;
    }

}
